package com.mx.zb.mng.ctrl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.mx.zb.mng.bo.Agent;
import com.mx.zb.mng.bo.Note;
import com.mx.zb.mng.bo.Tenant;
import com.mx.zb.mng.dao.AgentMapper;
import com.mx.zb.mng.dao.NoteMapper;
import com.mx.zb.mng.dao.TenantMapper;
import com.mx.zb.mng.utils.AjaxResult;
import com.mx.zb.mng.utils.Constant;
import com.mx.zb.mng.utils.db2.Page;
import com.mx.zb.mng.utils.db2.PageHelper;

@RestController
@RequestMapping("/i/note")
public class INoteCtrl {

	@Autowired
	NoteMapper noteMapper;

	@Autowired
	AgentMapper agentMapper;
	
	@Autowired
	TenantMapper  tenantMapper;

	// 商户读取这边接口

	// 参数:商户id
	// 商户所在行业,模版
	// 商户最后读取时间--到当前请求时间段内产生新的公告
	@RequestMapping(value = { "/reader/getNote" }, method = { RequestMethod.POST })
	public AjaxResult getNotReaderNote(@RequestBody JSONObject params) {
		Tenant o = params.getObject("Tenant", Tenant.class);// 最后读取时间
		String isRead = (String) params.get("IsRead");
		String label = (String) params.get("label"); // 
		String title = (String) params.get("title"); // 
		Note note = new Note();
		note.setCreateDate(o.getEndReadNote());
		note.setTemplates(o.getTemplateId());
		note.setTarget("商户");
		note.setTitle(title);
		note.setAgentId(null); //
		Page<Note> listnote = new Page<Note>();
		if(label!=null) {
			note.setLabel(label);
		}
		PageHelper.startPage();
		note.setCreatDate1(o.getCreateDate());
		if ("1".equals(isRead)) { // 已读
			listnote = (Page<Note>) noteMapper.getReaderNote(note);
		}else if("2".equals(isRead)) {//全部
			note.setCreateDate(null);
			listnote = (Page<Note>) noteMapper.getAllNote(note);
		}
		else {
			listnote = (Page<Note>) noteMapper.getNotReaderNote(note);
		}
		
		//过滤商户创建之前的公告信息
		/*if(listnote.size()>0) {
			for (int i = 0; i < listnote.size(); i++) {
				if(o.getCreateDate().getTime()-listnote.get(i).getCreateDate().getTime()>=0) {
					listnote.remove(listnote.get(i));
					 i--;  
					 continue;
				}
				
			}
		}*/
		return new AjaxResult().success(listnote);
	}

	@RequestMapping(value = { "/reader/getReader" }, method = { RequestMethod.POST })
	public AjaxResult readerNote(@RequestBody JSONObject params) {
		String noteId = params.getString("noteId");// 最后读取时间
		Note note = noteMapper.selectByPrimaryKey(noteId);
		return new AjaxResult().success(note);
	}
	
	//定时未读上级公告
	@RequestMapping(value = { "/reader/getNoteTimer" }, method = { RequestMethod.POST })
	@Transactional
	public AjaxResult getNoteTimer(@RequestBody JSONObject params) {
		Tenant o = params.getObject("Tenant", Tenant.class);// 最后读取时间
	Tenant tn = tenantMapper.selectByPrimaryKey(o.getId());
	List<Note> lnotes =new ArrayList<Note>();
	Note note = new Note();
	note.setCreateDate(o.getEndReadNote());
	note.setTemplates(o.getTemplateId());
	note.setTarget("商户");
	note.setCreatDate1(o.getCreateDate());
	if(tn!=null) {
		//查询上级
		Agent a = agentMapper.selectByPrimaryKey(tn.getAgentId());//代理商信息
		       if(a!=null) {
/*		    	   String leCode = a.getLevelCode();
*/		    	   String type=a.getType();
		    	   if(! Constant.headquarters.equals(type)) {
		    		   //说明上级不是总部
		    			//note.setAgentId(tn.getAgentId());
		    			List<Note> listnotes = 	noteMapper.getNotTimerNote(note);
		    			if(listnotes!=null) {
		    				for (int i = 0; i < listnotes.size(); i++) {
		    					lnotes.add(listnotes.get(i));
							}
		    			}
		    	    //加上总部的
		    	    Agent ag = new Agent();
		    	    ag.setType( Constant.headquarters);
		    	Agent   agt =    agentMapper.findAgents(ag);
		    	if(agt!=null) {
	    			//note.setAgentId(tn.getAgentId());
	    			List<Note>  lnotes1 = 	noteMapper.getNotTimerNote(note);
	    	    if(lnotes1!=null) {
    				for (int i = 0; i < lnotes1.size(); i++) {
    					lnotes.add(lnotes1.get(i));
					}
    			}
		    	}
		    	   }else {//上级是总部，
		    			//note.setAgentId(tn.getAgentId());
		    			List<Note>  lnotes2 = 	noteMapper.getNotTimerNote(note);
		    	      if(lnotes2!=null) {
		    				for (int i = 0; i < lnotes2.size(); i++) {
		    					lnotes.add(lnotes2.get(i));
							}
		    			}
		    	   }
		    	   
		       }
		     /*  if(lnotes.size()>0) {
					for (int i = 0; i < lnotes.size(); i++) {
						if(o.getCreateDate().getTime()-lnotes.get(i).getCreateDate().getTime()>=0) {
							lnotes.remove(lnotes.get(i));
							 i--;  
							 continue;
						}
						
					}
				}*/
		
	}
	return new AjaxResult().success(lnotes);
		
	}

}
